package com.panovue.dataextraction.reader;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.util.ExceptionUtils;
import com.panovue.dataextraction.ReaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

@Slf4j
public class SCBTReader extends AbstractReader {


    public SCBTReader() {
        super("SCBT", true);
    }

    public SCBTReader(String key) {
        super("SCBTt", true);
        this.key = key;

        baseUrl = "https://www.scbt.com/";
        baseDir += key + File.separator + "SCBT" + File.separator;

        buildDriver(true);

        addSeedAndReturn(baseUrl + "zh/search?Ntt=" + key).type("list");
        // 设置线程数
        setThreads(5);
        // 开始爬取
        try {
            start(3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void visit(Page page, CrawlDatums next) {

    }

    @MatchType(types = "list")
    public void list(Page page, CrawlDatums next) {
        String href = page.select("tbody td a").attr("href");
        next.addAndReturn(baseUrl + href).type("details");
    }

    @MatchType(types = "details")
    public void details(Page page, CrawlDatums next) {
        String href = page.select("a:contains(说明书)").attr("href");
        ReaderUtil.saveFile(baseUrl + href, baseDir + File.separator + "说明书.pdf");
    }


    @MatchCode(codes = {301, 302, 307})
    public void visitRedirect(Page page, CrawlDatums next) {
        try {
            String redirectUrl = new URL(new URL(page.url()), page.location()).toExternalForm();
            next.addAndReturn(redirectUrl).meta("type", "details");
        } catch (MalformedURLException e) {
            ExceptionUtils.fail(e);
        }
    }

    @Override
    public Reader buildReader(String key) {
        return new SCBTReader(key);
    }


    @Override
    public void afterStop() {
        if (driver != null) {
            driver.quit();
        }
        stop();
        log.info("SCB 数据爬取完成");
    }

}
